<?php

require_once("includes/sparqllib.php");
require_once("model/Dimension.class.php");
require_once("model/DimensionValue.class.php");
require_once("model/QueryManager.class.php");
Class dimensionController Extends baseController {

public $queryManager;

public function index() 
{
        
        $this->registry->template->show('dimension_index');
		
		
}

public function view(){
 
        $this->queryManager=QueryManager::GetInstance();
       $this->queryManager->iniConnection($this->parameters['endpoint']);
      
        $dimensionAndValues=$this->getDimensionMetadata($this->parameters['uri']);
        if(isset($dimensionAndValues['dimension'])){
			 echo '{"status":"ok","dimension":'.json_encode($dimensionAndValues['dimension']).' , "values" : '.json_encode($dimensionAndValues['values']).' , "numbers" : '.json_encode($dimensionAndValues['numbers']).'}';
        }
        else{ 
		
          echo '{"status":"error","msg":"No dimension exists..."}';
 		 }     

}

public function valueobervation(){
 
        $this->queryManager=QueryManager::GetInstance();
       $this->queryManager->iniConnection($this->parameters['endpoint']);
      
        $observations=$this->getDimensionValueObservation($this->parameters['uri'],$this->parameters['dimension']);
        if(isset($observations)){
			 echo '{"status":"ok","observations":'.json_encode($observations).'}';
        }
        else{ 
		
          echo '{"status":"error","msg":"No dimension exists..."}';
 		 }     

}


public function allvalues(){
 
        $this->queryManager=QueryManager::GetInstance();
       $this->queryManager->iniConnection($this->parameters['endpoint']);
        $dimensionAndValues=$this->getDimensionMetadata($this->parameters['uri']);
		
        if(isset($dimensionAndValues['dimension'])){
			 echo '{"status":"ok","values" : '.json_encode($dimensionAndValues['values']).'}';
        }
        else{ 
		
          echo '{"status":"error","msg":"No dimension exists..."}';
 		 }     

}
public function getDimensionValueObservation($val_uri,$dimension){
		$this->queryManager=QueryManager::GetInstance();
 
		$sparql=$this->queryManager->getNoObservationOfDimensionValueQuery($val_uri,$dimension);
		
    	$result = sparql_query( $sparql ); 
        
     	
		

		if( !$result ) { print sparql_errno() . ": " . sparql_error(). "\n"; exit; }
		$observations=array();
		while($row = sparql_fetch_array( $result )){
			$observations[]=$row['obser'];
		}
        //print_r($observations);
		return  $observations;
}
public function getDimensionMetadata($dim_uri){
        //TODO : discuss the attachment level 
		$this->queryManager=QueryManager::GetInstance();
 
		$sparql=$this->queryManager->getDimensionMetadataQuery($dim_uri);
		
    	$result = sparql_query( $sparql ); 
        
     	$dimensionAndValues=array('dimension'=>NULL,'values'=>array(),'numbers'=>array());
		

		if( !$result ) { print sparql_errno() . ": " . sparql_error(). "\n"; exit; }
		
		while($row = sparql_fetch_array( $result )){
		
		$dimensionLabel=(isset($row['dimensionLabel'])) ? $row['dimensionLabel'] : "N/A";   ;

        $valueuri=(isset($row['valueuri'])) ? $row['valueuri'] : "N/A";  

		$valuelabel=(isset($row['valuelabel'])) ? $row['valuelabel'] : "N/A";
		$comment=(isset($row['comment'])) ? $row['comment'] : "no data available";
    
		$dim=Dimension::WrapDimention($dim_uri,$dimensionLabel,$comment,NULL,NULL);
		$dimensionAndValues['dimension']=$dim;
		

	    $dimval=DimensionValue::WrapDimentionValue($valuelabel,$valueuri,"no data available");
		$dimensionAndValues['values'][]=$dimval;
		}
		
		/*for($i=0;$i<count($dimensionAndValues['values']);$i++){
		$valueuri=$dimensionAndValues['values'][$i]->valueURI;
		$sparql=$this->queryManager->getNoObservationOfDimensionValueQuery($valueuri);
    	$result = sparql_query( $sparql ); 
		$dimensionAndValues['numbers'][$i]=sparql_num_rows( $result );
		}*/
	return $dimensionAndValues;
}

}
?>
